package com.chenglin.mapdemo.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.chenglin.mapdemo.DisplayUtil;
import com.chenglin.mapdemo.R;
import com.chenglin.mapdemo.adapter.SearchAddressAdapter;
import com.chenglin.mapdemo.bean.ResultBean;
import com.chenglin.mapdemo.adapter.SearchTipsAdapter;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 搜索弹窗  使用 两个RecyclerView 实现的  顶部recyclerView用来实现推荐的tips列表 底部rv用来展示地址
 */
public class SearchDialog extends Dialog {

    @BindView(R.id.rv_tips)
    RecyclerView rvTips;
    @BindView(R.id.rv_result)
    RecyclerView rvResult;
    private Context mContext;
    private List<String> listTips;
    private List<ResultBean> listResult;
    private SearchCallBack callBack;
    private SearchTipsAdapter searchTipsAdapter;
    private SearchAddressAdapter searchResultAdapter;

    public SearchDialog(@NonNull Context context,List<String> listTips,List<ResultBean> listResult,SearchCallBack callBack) {
        super(context, R.style.DialogStyle);
        this.mContext = context;
        this.listTips = listTips;
        this.listResult = listResult;
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_search);
        ButterKnife.bind(this);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        window.setGravity(Gravity.TOP | Gravity.LEFT);
        int top = DisplayUtil.dip2px(mContext, (48+20+14));  //计算弹窗距离顶部距离 搜索框高度+搜索框距离顶部的距离+dialog距离搜索框的距离
        int left = DisplayUtil.dip2px(mContext, 20);  //计算弹窗距离左边距离
        window.getDecorView().setPadding(left, top, 0, 0);
        attributes.width = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setDimAmount(0f);
        window.setAttributes(attributes);
        initTipsView();
        initAddressView();
    }

    /**
     * 初始化地址的view
     */
    private void initAddressView() {
        rvResult.setLayoutManager(new LinearLayoutManager(mContext));
        if (searchResultAdapter == null){
            searchResultAdapter = new SearchAddressAdapter(mContext, listResult, new SearchAddressAdapter.ResultCallBack() {
                @Override
                public void onClickCallBack(int position) {
                    callBack.onAddressCallBack(listResult.get(position));
                    dismiss();
                }
            });
        }

        rvResult.setAdapter(searchResultAdapter );
    }

    /**
     * 初始化 推荐view
     */
    private void initTipsView() {
        rvTips.setLayoutManager(new GridLayoutManager(mContext,2,GridLayoutManager.HORIZONTAL,false){
            @Override
            public boolean canScrollVertically() {
                //禁止rv竖向滑动
                return false;
            }

        });

        if (searchTipsAdapter == null){
            searchTipsAdapter = new SearchTipsAdapter(mContext, listTips, new SearchTipsAdapter.TipsCallBack() {
                @Override
                public void onClickCallBack(int position) {
                    callBack.onTipsCallBack(listTips.get(position));

                }
            });
        }
        rvTips.setAdapter( searchTipsAdapter);
    }




    public interface SearchCallBack{
        void onTipsCallBack(String tips);  //推荐分类 回调
        void onAddressCallBack(ResultBean bean);  //搜索地址 回调
    }
}
